<?php

namespace App\Web\Data;

class Data_ScrumTasks extends \Lib\Data {
    function __construct() {
        parent::__construct();
    }

    public function getTableName() {
        return 'scrum_tasks';
    }

    /**
     * 获取信息
     *
     * @date   2018-08-01
     * @param int $taskId
     * @return mixed
     */
    public function getTaskInfoById($taskId) {
        $sql = "SELECT * FROM " . $this->getTableName() . " WHERE id = :id";
        $this->prepare($sql);
        $this->bind(':id', $taskId);
        $ret = $this->single();

        return $ret;
    }

    /**
     * 获取任务列表
     *
     * @date   2018-08-03
     * @param int $status 状态
     * @param int $sid    冲刺id
     * @return mixed
     */
    public function getTasksByStatus($status = 0, $sid = 0) {

        $whereStr = " WHERE status = :status";

        if ($sid) {
            $whereStr .= " AND sid = :sid";
        }

        $sql = "SELECT * FROM " . $this->getTableName() . $whereStr . " ORDER BY task_sort";
        $this->prepare($sql);

        $this->bind(':status', $status);
        if ($sid) {
            $this->bind(':sid', $sid);
        }

        $ret = $this->results();

        return $ret;
    }

    /**
     * 获取任务数量
     *
     * @date   2018-08-03
     * @param int $status 状态
     * @param int $sid    冲刺id
     * @return mixed
     */
    public function getTasksNumByStatus($status = 0, $sid = 0) {

        $whereStr = " WHERE status = :status";

        if ($sid) {
            $whereStr .= " AND sid = :sid";
        }

        $sql = "SELECT task_sort FROM " . $this->getTableName() . $whereStr. " ORDER BY task_sort desc limit 1";
        $this->prepare($sql);

        $this->bind(':status', $status);
        if ($sid) {
            $this->bind(':sid', $sid);
        }

        $ret = $this->single();

        return empty($ret) ? 1 : $ret['task_sort'] + 1;
    }

    /**
     * 添加
     *
     * @date   2018-08-01
     * @param array $data
     * @return array
     */
    public function addRow($data) {
        $ret = [];
        if (empty($data)) {
            return $ret;
        }

        $ret = $this->insertRow($data);

        return $ret;
    }

    /**
     * 修改
     *
     * @date   2018-08-01
     * @param array $where
     * @param array $data
     * @return array
     */
    public function editRowById($where, $data) {
        $ret = [];
        if (empty($where) || empty($data)) {
            return $ret;
        }

        $ret = $this->edit($where, $data);

        return $ret;
    }


    /**
     * 修改
     *
     * @date   2018-08-01
     * @param array $where
     * @return bool
     */
    public function delRowById($where) {
        $ret = [];
        if (empty($where)) {
            return $ret;
        }

        $ret = $this->delete($where);

        return $ret;
    }

}